package gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import commClient.TCPClientProxy;

import java.awt.CardLayout;

import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

		//Mainframe container 
public class Mainframe extends JFrame {

	public static final String LOGIN = "name_18065880174879";
	public static final String MENU = "name_17998030392209";
	public static final String DEPOSIT = "name_18090486896293";
	public static final String TRANSFER = "name_18090486896298";
	public static final String RECEIPT = "name_18114692592948";

	private JPanel contentPane;
	private Login login;
	private Menu menu;
	private Deposit deposit;
	private TransferToUser transferToUser;
	private Receipt receipt;
	private Icon helpicon;
	private Icon mailicon;
	private TCPClientProxy TCPClient;
	String amount;
	double amountD;

	private CardLayout c;

	public Mainframe() {
		setTitle("BCBS");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 630, 950);
		setResizable(false);
		Dimension centerScreen = Toolkit.getDefaultToolkit().getScreenSize();
		this.setLocation(centerScreen.width / 2 - this.getSize().width / 2,
				centerScreen.height / 2 - this.getSize().height / 2);
		contentPane = new JPanel();
		setContentPane(contentPane);
		contentPane.setLayout(new CardLayout(0, 0));

		//Login screen
		login = new Login();
		login.setFocusable(true);
		login.getHelpCenter().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent pidgey) {
				login.requestFocus();

				helpicon = new ImageIcon(getClass().getResource(
						"Question_icon-small.png"));
				JOptionPane
						.showMessageDialog(
								null,
								"Please find the correct help service for your needs:\n\nIf you have forgotten your"
										+ " password, please contact the CBS IT Helpdesk department.\nFor help with your Bitcoin account"
										+ ", please refer to the support page of your Bitcoin wallet provider.\n"
										+ "For assistance in using this app, meet us at the BCBS helpdesk in Solbjerg Plads.",
								"Help Center", JOptionPane.PLAIN_MESSAGE,
								helpicon);

			}
		});
		
		//Login button actionListener function
		login.getLogin().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (login.getTextfieldEMAIL().getText().equals("")
						|| login.getPasswordfieldPASSWORD().getText()
								.equals("")) {
					System.out
							.println("Response: No. User is not authenticated.");
					login.getError().setVisible(true);
				} else {
					String mail = login.getTextfieldEMAIL().getText();
					String pass = login.getPasswordfieldPASSWORD().getText();
					String logindetails = new String("AUTH:" + mail + ":"
							+ pass);
					System.out.println("Step 1. This is sent to TCPClient: "
							+ logindetails);

					TCPClient = new TCPClientProxy(logindetails);

					System.out
							.println("Step 11. 'yes'/'no' goes from AfterDecryption -> TCPClientProxy -> Mainframe: "
									+ TCPClient.getReply());

					try {
						if (TCPClient.getReply().equals("yes")) {
							System.out
									.println("Response: Yes. User Authenticated.");
							menu.getWelcome()
									.setText(
											"Welcome "
													+ TCPClient.getReplyName()
													+ "!");
							menu.getCurrencyBTC().setText(
									TCPClient.getReplyBalanceBTC() + " BTC");
							menu.getCurrencyDKK().setText(
									TCPClient.getReplyBalanceDKK() + " DKK");

							c.show(contentPane, MENU);
							login.getError().setVisible(false);
						} else {
							System.out
									.println("Response: No. User is not authenticated.");
							login.getError().setVisible(true);
							login.getPasswordfieldPASSWORD().setText("");
							login.getPasswordfieldPASSWORD().requestFocus();
						}
					} catch (Exception e) {
					}
				}

			}
		});
		
		//Pass field focusListener function
		login.getPasswordfieldPASSWORD().addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent dragonite) {
				if (login.getPasswordfieldPASSWORD().getText()
						.equals("Password"))
					login.getPasswordfieldPASSWORD().setText("");
				login.getPasswordfieldPASSWORD().setForeground(
						new Color(0, 0, 0));
				login.getPasswordfieldPASSWORD().setEchoChar('*');
			}

			public void focusLost(FocusEvent charmander) {
				if (login.getPasswordfieldPASSWORD().getText().equals("")) {
					login.getPasswordfieldPASSWORD().setForeground(
							new Color(192, 192, 192));
					login.getPasswordfieldPASSWORD().setText("Password");
					login.getPasswordfieldPASSWORD().setEchoChar((char) 0);
				} else {
					login.getPasswordfieldPASSWORD().setEchoChar('*');
				}
			}
		});
		
		//Email field focusListener function
		login.getTextfieldEMAIL().addFocusListener(new FocusAdapter() {
			public void focusGained(FocusEvent raichu) {
				if (login.getTextfieldEMAIL().getText().equals("CBS Email")) {
					login.getTextfieldEMAIL().setText("");
					login.getTextfieldEMAIL().setForeground(new Color(0, 0, 0));
				}
			}

			public void focusLost(FocusEvent mewtwo) {
				if (login.getTextfieldEMAIL().getText().equals("")) {
					login.getTextfieldEMAIL().setForeground(
							new Color(192, 192, 192));
					login.getTextfieldEMAIL().setText("CBS Email");
				}
			}

		});
		login.getPasswordfieldPASSWORD().setEchoChar((char) 0);
		contentPane.add(login, "name_18065880174879");

		
//		Menu screen run
		menu = new Menu();
		menu.setFocusable(true);
		
//		Menu Deposit button actionListener function
		menu.getTransfer().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent articuno) {
				c.show(contentPane, DEPOSIT);
			}
		});
		
//		Menu Transfer button actionListener function
		menu.getBtnTransferToUser().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent transfer) {
				c.show(contentPane, TRANSFER);
			}
		});
		
//		Menu Logout button actionListener function
		menu.getLogout().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent sandshrew) {
				login.getPasswordfieldPASSWORD().setText("");
				c.show(contentPane, LOGIN);
			}
		});
		contentPane.add(menu, "name_17998030392209");

//		Deposit screen run and a lot of number actionListener
		deposit = new Deposit();
		deposit.setFocusable(true);
		deposit.get0().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent snorlax) {
				deposit.getAmount()
						.setText(deposit.getAmount().getText() + "0");
			}
		});
		deposit.get1().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent snorlax) {
				deposit.getAmount()
						.setText(deposit.getAmount().getText() + "1");
			}
		});
		deposit.get2().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent snorlax) {
				deposit.getAmount()
						.setText(deposit.getAmount().getText() + "2");
			}
		});
		deposit.get3().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent snorlax) {
				deposit.getAmount()
						.setText(deposit.getAmount().getText() + "3");
			}
		});
		deposit.get4().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent snorlax) {
				deposit.getAmount()
						.setText(deposit.getAmount().getText() + "4");
			}
		});
		deposit.get5().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent snorlax) {
				deposit.getAmount()
						.setText(deposit.getAmount().getText() + "5");
			}
		});
		deposit.get6().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent snorlax) {
				deposit.getAmount()
						.setText(deposit.getAmount().getText() + "6");
			}
		});
		deposit.get7().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent snorlax) {
				deposit.getAmount()
						.setText(deposit.getAmount().getText() + "7");
			}
		});
		deposit.get8().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent snorlax) {
				deposit.getAmount()
						.setText(deposit.getAmount().getText() + "8");
			}
		});
		deposit.get9().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent snorlax) {
				deposit.getAmount()
						.setText(deposit.getAmount().getText() + "9");
			}
		});
		
//		Comma action function
		deposit.getComma().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent snorlax) {
				if (deposit.getAmount().getText().equals("")) {
					deposit.getAmount().setText("0.");
				} else if (deposit.getAmount().getText().contains(".")) {
					deposit.getAmount().setText(
							deposit.getAmount().getText() + "");
				} else {
					deposit.getAmount().setText(
							deposit.getAmount().getText() + ".");
				}

			}
		});
		
//		Cancel button function
		deposit.getCancel().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent jigglypuff) {
				deposit.getAmount().setText("");
				c.show(contentPane, MENU);
			}
		});
		
//		Delete button function
		deposit.getDel().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent rattata) {
				String currentString = deposit.getAmount().getText();
				if (currentString.equals("")) {
					// Do nothing
				} else {
					String newString = removelastChar(currentString);
					deposit.getAmount().setText(newString);
				}

			}
		});
		
//		OK button action function
		deposit.getOK().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent rattata) {
				String thisamount = deposit.getAmount().getText();
				double disamount = Double.parseDouble(thisamount);

				int depositWindow = JOptionPane.showConfirmDialog(null,
						"You are about to transfer the following amount:\n\n"
								+ disamount + " BTC", "Confirm deposit",
						JOptionPane.YES_NO_OPTION);

				if (depositWindow == JOptionPane.YES_OPTION) {
					String mail = login.getTextfieldEMAIL().getText();
					String pass = login.getPasswordfieldPASSWORD().getText();
					String transferdetails = new String("TRANSFER:" + mail
							+ ":" + pass + ":" + disamount);
					System.out.println("This is sent to TCPClient: "
							+ transferdetails);

					TCPClient = new TCPClientProxy(transferdetails);

					try {
						if (TCPClient.getReply().equals("confirmed")) {
							System.out
									.println("Transfer confirmed. Reply was: confirmed");
							menu.getCurrencyBTC().setText(
									TCPClient.getReplyNewBalanceBTC() + " BTC");
							menu.getCurrencyDKK().setText(
									TCPClient.getReplyNewBalanceDKK() + " DKK");
							receipt.getAmount().setText(disamount + " BTC");
							c.show(contentPane, RECEIPT);
						} else {
							System.out
									.println("There was an error updating the database..");
						}
					} catch (Exception e) {
					}
				} else {
					// do nothing
				}

			}
		});
		contentPane.add(deposit, "name_18090486896293");

//		TransferToUser screen run and number actionsListeners just like deposit above 
		transferToUser = new TransferToUser();
		transferToUser.setFocusable(true);
		transferToUser.getTtu0().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent snorlax) {
				transferToUser.getTtuAmount().setText(
						transferToUser.getTtuAmount().getText() + "0");
			}
		});
		transferToUser.getTtu1().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent snorlax) {
				transferToUser.getTtuAmount().setText(
						transferToUser.getTtuAmount().getText() + "1");
			}
		});
		transferToUser.getTtu2().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent snorlax) {
				transferToUser.getTtuAmount().setText(
						transferToUser.getTtuAmount().getText() + "2");
			}
		});
		transferToUser.getTtu3().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent snorlax) {
				transferToUser.getTtuAmount().setText(
						transferToUser.getTtuAmount().getText() + "3");
			}
		});
		transferToUser.getTtu4().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent snorlax) {
				transferToUser.getTtuAmount().setText(
						transferToUser.getTtuAmount().getText() + "4");
			}
		});
		transferToUser.getTtu5().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent snorlax) {
				transferToUser.getTtuAmount().setText(
						transferToUser.getTtuAmount().getText() + "5");
			}
		});
		transferToUser.getTtu6().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent snorlax) {
				transferToUser.getTtuAmount().setText(
						transferToUser.getTtuAmount().getText() + "6");
			}
		});
		transferToUser.getTtu7().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent snorlax) {
				transferToUser.getTtuAmount().setText(
						transferToUser.getTtuAmount().getText() + "7");
			}
		});
		transferToUser.getTtu8().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent snorlax) {
				transferToUser.getTtuAmount().setText(
						transferToUser.getTtuAmount().getText() + "8");
			}
		});
		transferToUser.getTtu9().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent snorlax) {
				transferToUser.getTtuAmount().setText(
						transferToUser.getTtuAmount().getText() + "9");
			}
		});
		transferToUser.getTtuComma().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent snorlax) {
				if (transferToUser.getTtuAmount().getText().equals("")) {
					transferToUser.getTtuAmount().setText("0.");
				} else if (transferToUser.getTtuAmount().getText()
						.contains(".")) {
					transferToUser.getTtuAmount().setText(
							transferToUser.getTtuAmount().getText() + "");
				} else {
					transferToUser.getTtuAmount().setText(
							transferToUser.getTtuAmount().getText() + ".");
				}

			}
		});
		transferToUser.getTtuCancel().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent jigglypuff) {
				transferToUser.getTtuAmount().setText("");
				c.show(contentPane, MENU);
			}
		});
		transferToUser.getTtuDel().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent rattata) {
				String currentStringTtu = transferToUser.getTtuAmount()
						.getText();
				if (currentStringTtu.equals("")) {
					// Do nothing
				} else {
					String newStringTtu = removelastChar(currentStringTtu);
					transferToUser.getTtuAmount().setText(newStringTtu);
				}

			}
		});
		transferToUser.getTtuOK().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent rattata) {
				String thisamountTtu = transferToUser.getTtuAmount().getText();
				double disamountTtu = Double.parseDouble(thisamountTtu);

				int depositWindowTtu = JOptionPane.showConfirmDialog(null,
						"You are about to transfer the following amount:\n\n"
								+ disamountTtu + " BTC", "Confirm deposit",
						JOptionPane.YES_NO_OPTION);

				if (depositWindowTtu == JOptionPane.YES_OPTION) {
					String mail = login.getTextfieldEMAIL().getText();
					String pass = login.getPasswordfieldPASSWORD().getText();
					String TransferDestination = transferToUser.getTxtEmailTo()
							.getText();
					String transferToUserdetails = new String("TRANSFERTOUSER:"
							+ mail + ":" + pass + ":" + TransferDestination
							+ ":" + disamountTtu);
					System.out.println("This is sent to TCPClient: "
							+ transferToUserdetails);

					TCPClient = new TCPClientProxy(transferToUserdetails);

					try {
						if (TCPClient.getReply().equals("YES")) {
							System.out
									.println("Transfer confirmed. Reply was: confirmed");
							menu.getCurrencyBTC().setText(
									TCPClient.getReplyNewBalanceBTC() + " BTC");
							menu.getCurrencyDKK().setText(
									TCPClient.getReplyNewBalanceDKK() + " DKK");
							receipt.getAmount().setText(disamountTtu + " BTC");
							c.show(contentPane, RECEIPT);
						} else {
							System.out
									.println("Response: No. User is not authenticated.");
							transferToUser.getLblTheEmailYou()
									.setVisible(false);
							transferToUser.getLblError().setVisible(true);
							transferToUser.getLblAmountError().setVisible(true);
							System.out
									.println("There was an error updating the database..");
						}
					} catch (Exception e) {
					}
				} else {
					// do nothing
				}

			}
		});
		contentPane.add(transferToUser, "name_18090486896298");

//		Receipt screen runs
		receipt = new Receipt();
		receipt.setFocusable(true);
		receipt.getBack().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				deposit.getAmount().setText("");
				transferToUser.getLblTheEmailYou().setVisible(true);
				transferToUser.getLblError().setVisible(false);
				transferToUser.getLblAmountError().setVisible(false);
				transferToUser.getTtuAmount().setText("");
				transferToUser.getTxtEmailTo().setText("");
				c.show(contentPane, MENU);
			}
		});
		
//		Send mail button action function
		receipt.getBtnSendToMail().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String mail = login.getTextfieldEMAIL().getText();
				String pass = login.getPasswordfieldPASSWORD().getText();
				String dateTimetxt = receipt.getLblDateTime().getText();
				String dateTime = receipt.getLblTimeDateRep().getText();
				String copyright = receipt.getLblCopyright().getText();
				if (transferToUser.getTtuAmount().getText().equals("")) {
					amount = deposit.getAmount().getText();
					amountD = Double.parseDouble(amount);
				} else {
					amount = transferToUser.getTtuAmount().getText();
					amountD = Double.parseDouble(amount);
				}
				System.err.println("THIS IS AMOUNT TO SEND TO RECEIPT: "
						+ amount);
				String receiptdata = new String("RECEIPT:" + mail + ":" + pass
						+ ":" + dateTimetxt + ":" + dateTime + ":" + copyright
						+ ":" + amountD);

				TCPClient = new TCPClientProxy(receiptdata);
				if (TCPClient.getReply().equals("sent")) {
					mailicon = new ImageIcon(getClass().getResource(
							"MailSend.png"));
					JOptionPane
							.showMessageDialog(
									null,
									"A receipt has successfully been sent to your assigned E-mail address.",
									null, JOptionPane.PLAIN_MESSAGE, mailicon);
				}
			}
		});

		contentPane.add(receipt, "name_18114692592948");

		c = (CardLayout) getContentPane().getLayout();
	}

	public Login getLogin() {
		return login;
	}

	public Menu getMenu() {
		return menu;
	}

	public Deposit getDeposit() {
		return deposit;
	}

	public void show(String card) {
		c.show(this.getContentPane(), card);
	}

	public static String removelastChar(String str) {
		return str.substring(0, str.length() - 1);
	}
}
